// index.js
import TencentCloudChat from '@tencentcloud/chat';
import TIMUploadPlugin from 'tim-upload-plugin';
import TIMProfanityFilterPlugin from 'tim-profanity-filter-plugin';
import { genTestUserSig } from '../../debug/GenerateTestUserSig';
Page({
  data: {
    motto: '员工版',
    userInfo: {
    },
    hasUserInfo: false,
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    webUrl: '',
    phone: '',
    password: ''
  },
  onChooseAvatar(e) {
    // const { avatarUrl } = e.detail
    // const { nickName } = this.data.userInfo
    // this.setData({
    //   "userInfo.avatarUrl": avatarUrl,
    //   hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    // })
  },
  onInputChange(e) {
    // const nickName = e.detail.value
    // const { avatarUrl } = this.data.userInfo
    // this.setData({
    //   "userInfo.nickName": nickName,
    //   hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    // })
  },
  loginTim(userID) {
      wx.$TUIKit = TencentCloudChat.create({
        SDKAppID: getApp().globalData.config.SDKAPPID,
      });
      getApp().globalData.config.userID = userID;
      const userSig = genTestUserSig( getApp().globalData.config).userSig 
      wx.$chat_SDKAppID =  getApp().globalData.config.SDKAPPID;
      wx.TencentCloudChat = TencentCloudChat;
      wx.$chat_userID = userID;
      wx.$chat_userSig = userSig;
      wx.$TUIKit.registerPlugin({ 'tim-upload-plugin': TIMUploadPlugin });
      wx.$TUIKit.registerPlugin({ 'tim-profanity-filter-plugin': TIMProfanityFilterPlugin });
      wx.$TUIKit.login({
        userID: userID,
        userSig
    });
      // 监听系统级事件
    wx.$TUIKit.on(wx.TencentCloudChat.EVENT.SDK_READY, this.onSDKReady,this);
  },
  onUnload() {
    wx.$TUIKit.off(wx.TencentCloudChat.EVENT.SDK_READY, this.onSDKReady,this);
  },
  onSDKReady(event) {
    // 监听到此事件后可调用 SDK 发送消息等 API，使用 SDK 的各项功能。
    console.log("SDK ready");
  },
  onLoad:function(){
    const app = getApp(); // 获取App的实例
    const that = this;
    this.data.webUrl = app.globalData.onlineService;
    console.log(this.data.webUrl);
        // 登录
        wx.login({
          success: res => {
            // 发送 res.code 到后台换取 openId, sessionKey, unionId
            if (res.code) {
              // 发起网络请求，将code发送到服务器
              wx.request({
                url:  this.data.webUrl + '/api/girls/login',
                method: 'POST',
                data: {
                  code: res.code
                },
                success(res) {
                  console.log('服务器返回:', res.data);
                  if(res.data.code === 200) {
                    wx.setStorageSync('userInfo', res.data.msg);
                    app.globalData.userInfo = res.data.msg;
                    // 登录成功，跳转到首页
                    wx.switchTab({
                      url: '/pages/home/home'
                    })
                    // wx.navigateTo({
                    //   url: '/pages/cerficate/cerficate'
                    // })
                    that.loginTim(res.data.msg.id.toString())  ;
                    // wx.redirectTo({
                    //   url: '/pages/publish/activity/activity'
                    // })
                  }else{
                    console.log('登录失败！' + res.data.msg);
                      // 登录失败，显示提示
                      wx.showToast({
                        title: '登录失败',
                        icon: 'error',
                        duration: 2000,
                        complete: () => {
                          // 2秒后退出小程序
                          setTimeout(() => {
                            wx.exitMiniProgram()
                          }, 2000)
                        }
                      })
                  }
                  
                }
              });
            } else {
              console.log('登录失败！' + res.errMsg);
              wx.showToast({
                title: '登录失败',
                icon: 'error',
                duration: 2000,
                complete: () => {
                  // 2秒后退出小程序
                  setTimeout(() => {
                    wx.exitMiniProgram()
                  }, 2000)
                }
              })
            }
          }
        })
  }
})
